package com.tyj.gulimall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.tyj.gulimall.product.feign.CouponFeignService;
import com.tyj.gulimall.product.vo.SpuSaveVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.tyj.gulimall.product.entity.SpuInfoEntity;
import com.tyj.gulimall.product.service.SpuInfoService;
import com.tyj.common.utils.PageUtils;
import com.tyj.common.utils.R;



/**
 * spu信息
 *
 * @author tanyoujie
 * @email 983629093@qq.com
 * @date 2021-02-09 19:09:40
 */

@Slf4j
@RequestMapping("product/spuinfo")
@RestController
public class SpuInfoController {
    @Autowired
    private SpuInfoService spuInfoService;

    @Autowired
    private CouponFeignService couponFeignService;

//    http://gulimall-product/product/spuinfo/skuId/22
    @RequestMapping("/skuId/{skuId}")
    public R getSpuBySkuId(@PathVariable("skuId") Long skuId) {
        log.debug("getSpuBySkuId方法执行成功 skuId={}",skuId);
        SpuInfoEntity spuInfoEntity = spuInfoService.getSpuBySkuId(skuId);
        return R.ok().setData(spuInfoEntity);
    }

    /**
     * spu上架商品
     * @return
     */
    @PostMapping("/{spuId}/up")
    public R spuUp(@PathVariable("spuId") Long spuId){
        spuInfoService.spuUp(spuId);
        return R.ok();
    }

    //远程调用测试
    @PostMapping("/RPCTest")
    private R RPCTest(){
        String s = couponFeignService.RPCTest();
        return R.ok().put("name",s);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:spuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = spuInfoService.queryByCondition(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:spuinfo:info")
    public R info(@PathVariable("id") Long id){
		SpuInfoEntity spuInfo = spuInfoService.getById(id);

        return R.ok().put("spuInfo", spuInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:spuinfo:save")
    public R save(@RequestBody SpuSaveVo vo){
//		spuInfoService.save(spuInfo);
      spuInfoService.saveSpuInfo(vo);
//        System.out.println("保存spuInfo信息="+vo);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:spuinfo:update")
    public R update(@RequestBody SpuInfoEntity spuInfo){
		spuInfoService.updateById(spuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:spuinfo:delete")
    public R delete(@RequestBody Long[] ids){
		spuInfoService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
